//
//  LXOptionButotn.m
//  LXGlobalMall
//
//  Created by charlicar on 16/5/17.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXOptionButton.h"
@interface LXOptionButton()
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)NSArray * paths;


@property(nonatomic,strong,readwrite)NSString * title;
@property(nonatomic,strong,readwrite)NSString * number;
@end

@implementation LXOptionButton

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.isPopButton = NO;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}

- (instancetype)init{
    if(self = [super init]){
        self.isPopButton = NO;
        self.style = LXOptionButtonStyleD_U;
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.isPopButton = NO;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setAdjustsImageWhenHighlighted:NO];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    switch (self.style) {
        case LXOptionButtonStyleU_D:{//字在上 图在下
            
        }break;
        case LXOptionButtonStyleD_U:{//字在下 图在上
            //设置图片位置
            self.imageView.y         = self.height * 0.1;
            self.imageView.height    = self.height * 0.5;
            self.imageView.width     = self.imageView.height;
            self.imageView.centerX   = self.width * 0.5;
            
            //设置文字位置
            self.titleLabel.x        = 0;
            self.titleLabel.y        = self.imageView.botton;
            self.titleLabel.width    = self.width;
            self.titleLabel.height   = self.height - self.titleLabel.y;
        }break;
        case LXOptionButtonStyleL_R:{//字在左 图在右
            CGSize size         = [self.titleLabel.text sizeInAre:self.bounds.size font:self.titleLabel.font];
            CGFloat imgHeight   = self.height*0.5;
            CGFloat imgWidth    = imgHeight;
            CGFloat imgX        = (self.width-(imgWidth+size.width))*0.5;
            //设置图片位置
            
            self.imageView.height    = imgHeight;
            self.imageView.width     =  imgWidth;
            self.imageView.centerY   = self.centerY;
            self.imageView.x         = imgX;
            
            //设置文字位置
            
            self.titleLabel.x        = self.imageView.right;
            self.titleLabel.centerY  = self.centerY;
            self.titleLabel.width    = size.width;
            self.titleLabel.height   = size.height;
  
        }break;
        case LXOptionButtonStyleR_L:{//字在右 图在左
            CGSize size             = [self.titleLabel.text sizeInAre:self.bounds.size font:self.titleLabel.font];
            self.titleLabel.width   = size.width;
            self.titleLabel.height  = size.height;
//            self.imageView.height   = self.height*0.5;
//            self.imageView.width    = self.imageView.height*0.5;
            self.imageView.size = self.imageView.image.size;
            self.titleLabel.x       = (self.width-size.width-self.imageView.width)*0.5;
            self.imageView.x        = self.titleLabel.right+2;
            self.imageView.centerY  = self.titleLabel.centerY;
        }break;
        case LXOptionButtonStyleTitleWithNumber:{
            [self setRadius:0];
            CGSize size1             = [self.titleLabel.text sizeInAre:self.bounds.size font:self.titleLabel.font];
            self.titleLabel.width   = size1.width;
            self.titleLabel.height  = size1.height;
    
            CGSize size2             = [self.numberLabel.text sizeInAre:self.bounds.size font:self.numberLabel.font];
            self.numberLabel.width   = size2.width;
            self.numberLabel.height  = size2.height;
            
            self.numberLabel.y          = self.height*0.4 - size2.height;
            self.titleLabel.y           = self.numberLabel.botton;
            self.numberLabel.centerX    = self.width*0.5;
            self.titleLabel.centerX     = self.width*0.5;
            
        }break;
            
        case LXOptionButtonStyleMineCenter:{//个人中心页面效果
            
            [self setRight:0];
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            
            self.backgroundColor = [UIColor whiteColor];
            //设置图片位置
            self.imageView.y         = self.height * 0.2;
            self.imageView.height    = self.height * 0.3;
            self.imageView.width     = self.imageView.height;
            self.imageView.centerX   = self.width * 0.5;
            
            //设置文字位置
            self.titleLabel.x        = 0;
            self.titleLabel.y        = self.imageView.botton;
            self.titleLabel.width    = self.width;
            self.titleLabel.height   = self.height - self.titleLabel.y;
            
            self.messegeLabel.width = 20;
            self.messegeLabel.height = 20;
            self.messegeLabel.x = self.width - 30;
            self.messegeLabel.y = 10;
        }
            
        default:{
            
        }break;
    }
}


-(void)setIsPopButton:(BOOL)isPopButton{
     _isPopButton = isPopButton;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    if(self.isPopButton){
        [self setRadius:0];
        UIBezierPath *path1 = [UIBezierPath bezierPath];
        UIBezierPath *path2 = [UIBezierPath bezierPath];
        UIBezierPath *path3 = [UIBezierPath bezierPath];
        
        [[UIColor blackColor] set];
        [path1 moveToPoint:CGPointMake(0, 0)];
        path1.lineWidth = 0.5;
        [path1 addLineToPoint:CGPointMake(0, self.height)];
        [path1 stroke];

        
        [[UIColor blackColor] set];
        [path2 moveToPoint:CGPointMake(self.width, 0)];
        path2.lineWidth = 0.5;
        [path2 addLineToPoint:CGPointMake(self.width, self.height)];
        [path2 stroke];
        
        [[UIColor blackColor] set];
        [path3 moveToPoint:CGPointMake(0, 0.2)];
        path3.lineWidth = 0.5;
        [path3 addLineToPoint:CGPointMake(self.width, 0.2)];
        [path3 stroke];
        
        self.paths = @[path1,path2,path3];
    }
    else{
        for (UIBezierPath * path in self.paths) {
            [path removeAllPoints];
        }
    }
}

-(void)setTitle:(NSString *)title number:(NSString*)number{
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor clearColor];
    self.title = title;
    self.number = number;
    self.titleLabel.text = title;
    self.numberLabel.font = [UIFont systemFontOfSize:12];
    self.numberLabel.textColor = [UIColor blackColor];
    self.numberLabel.text = number;
    [self addSubview:self.numberLabel];
    self.style = LXOptionButtonStyleTitleWithNumber;
}


-(UILabel*)numberLabel{
    if(_numberLabel==nil){
        _numberLabel  = [[UILabel alloc] initWithFrame:CGRectZero];
        _numberLabel.numberOfLines = 0;
        _numberLabel.backgroundColor = [UIColor clearColor];
        _numberLabel.font = [UIFont systemFontOfSize:12];
    }
    return _numberLabel;
}


-(UILabel*)messegeLabel
{
    if(_messegeLabel==nil)
    {
        _messegeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _messegeLabel.backgroundColor = [UIColor redColor];
        _messegeLabel.textColor = [UIColor whiteColor];
        _messegeLabel.font = [UIFont systemFontOfSize:12];
        _messegeLabel.textAlignment = NSTextAlignmentCenter;
        [_messegeLabel addRadius:5];
        [self addSubview:_messegeLabel];
    }
    return _messegeLabel;
}

-(void)setMessegeCount:(NSInteger)messegeCount
{
    _messegeCount = messegeCount;
    if (_messegeCount<=0) {
        self.messegeLabel.text = @"";
        self.messegeLabel.hidden = YES;
    }
    else
    {
        self.messegeLabel.hidden = NO;
        self.messegeLabel.text = [NSString stringWithFormat:@"%ld",_messegeCount];
    }
}

-(void)setStyle:(LXOptionButtonStyle)style{
    _style = style;
    [self setNeedsLayout];
}
@end
